#include "registerdialog.h"
#include "ui_registerdialog.h"
#include <QMessageBox>
#include <QFile>
#include <QRegularExpression>


RegisterDialog::RegisterDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::RegisterDialog)
{
    ui->setupUi(this);

    setWindowTitle("注册账号");
    ui->nicknameEdit->setPlaceholderText("输入昵称");
    ui->passwordEdit->setPlaceholderText("输入密码");
    ui->securityEdit->setPlaceholderText("输入密保");

    // 获取控件指针
    nicknameEdit = ui->nicknameEdit;
    passwordEdit = ui->passwordEdit;
    securityEdit = ui->securityEdit;
    agreeCheck = ui->agreeCheck;
    registerButton = ui->registerButton;

    // 初始禁用按钮
    registerButton->setEnabled(false);
    connect(nicknameEdit, &QLineEdit::textChanged, this, &RegisterDialog::checkEnable);
    connect(passwordEdit, &QLineEdit::textChanged, this, &RegisterDialog::checkEnable);
    connect(securityEdit, &QLineEdit::textChanged, this, &RegisterDialog::checkEnable);
    connect(agreeCheck, &QCheckBox::stateChanged, this, &RegisterDialog::checkEnable);

}

RegisterDialog::~RegisterDialog()
{
    delete ui;
}

void RegisterDialog::on_registerButton_clicked()
{
    // 这里可以添加注册逻辑
    QString nickname = nicknameEdit->text().trimmed();
    QString password = passwordEdit->text();
    QString security = securityEdit->text();

    if(nickname.isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "昵称不能为空！");
        return ;
    }
    if(password.isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "密码不能为空！");
        return ;
    }
    if (password.length() < 6 ) {
        QMessageBox::warning(this, "密码错误", "密码长度不能少于6位！");
        return;
    }
    // 使用正则表达式检查：必须包含字母和数字
    // 检查密码强度：必须包含字母和数字，且长度 >=6
    QRegularExpression rx("^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{6,}$");
    if(!rx.match(password).hasMatch())
    {
        QMessageBox::warning(this, "密码错误", "密码必须包含字母和数字，且不少于6位！");
        return;
    }
    if(security.isEmpty())
    {
        QMessageBox::warning(this, "输入错误", "密保问题不能为空！");
        return ;
    }


    // 2. 检查昵称是否已存在
    QString filePath = "users.txt";  // 保存用户信息的文件
    QFile file(filePath);

    if(file.open(QIODevice::ReadOnly))
    {
        QTextStream in(&file);
        QString line;
        while ((line = in.readLine()) != nullptr)
        {
            QStringList parts = line.split("||");
            if (parts.size() == 3 && parts[0] == nickname)
            {
                QMessageBox::warning(this, "注册失败", "该昵称已存在，请更换昵称！");
                file.close();
                return;
            }
        }
        file.close();
    }

    // 3. 写入新用户信息
    if(file.open(QIODevice::Append))
    {
        QTextStream out(&file);
        out << nickname << "||" << password << "||" << security << "\n";
        file.close();

        // 4. 提示注册成功
        QMessageBox::information(this, "注册成功", "欢迎使用监察系统！\n昵称："+ nickname);
        accept();  // 关闭对话框，返回 QDialog::Accepted
    }
    else
    {
        QMessageBox::critical(this, "错误", "无法写入用户文件，请检查权限！" );
    }
    // 示例：弹出提示
    //QMessageBox::information(this, "注册成功", "欢迎使用监察系统！");
    accept();  // 关闭对话框并返回 Accepted
}

void RegisterDialog::checkEnable()
{
    bool hasNickname = !nicknameEdit->text().isEmpty();
    bool hasPassword = !passwordEdit->text().isEmpty();
    bool hasSecurity = !securityEdit->text().isEmpty();
    bool agreed = agreeCheck->isChecked();

    registerButton->setEnabled(hasNickname && hasPassword && hasSecurity && agreed);
}

